﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScheduleMaker.DB.Domain;
using ScheduleMaker.DB.Domain.Core;

namespace ScheduleMaker.DB.CoreDAO
{
    public class UserDAO : CoreAbstractDAO<User>
    {
        private AdapterAbstractDAO<User, UserSchool> wrappedAbstractDAOSchool;
        private AdapterAbstractDAO<User, UserUniversity> wrappedAbstractDAOUniversity;
        private UserDAOSchool wrappedDAOSchool;
        private UserDAOUniversity wrappedDAOUniversity;

        public UserDAO(UserDAOSchool wrappedDAOSchool)
        {
            this.wrappedDAOSchool = wrappedDAOSchool;
            this.wrappedAbstractDAOSchool = 
                new AdapterAbstractDAO<User,UserSchool>((AbstractDAO<UserSchool>)wrappedDAOSchool);
        }

        public UserDAO(UserDAOUniversity wrappedDAOUniversity)
        {
            this.wrappedDAOUniversity = wrappedDAOUniversity;
            this.wrappedAbstractDAOUniversity =
                new AdapterAbstractDAO<User, UserUniversity>((AbstractDAO<UserUniversity>)wrappedDAOUniversity);
        }

        public User findByLogin(string login)
        {
            User user;
            if (wrappedDAOSchool != null)
            {
                user = wrappedDAOSchool.findByLogin(login);   
            }
            else if (wrappedDAOUniversity != null)
            {
                user = wrappedDAOUniversity.findByLogin(login);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ClassHourDAOSchool nor ClassHourDAOUniversity was initialized");
            }
            return user;
        }

        public override void update(User entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.update(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.update(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ClassHourDAOSchool nor ClassHourDAOUniversity was initialized");
            }
        }

        public override void save(User entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.save(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.save(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ClassHourDAOSchool nor ClassHourDAOUniversity was initialized");
            }
        }

        public override void saveOrUpdate(User entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.saveOrUpdate(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.saveOrUpdate(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ClassHourDAOSchool nor ClassHourDAOUniversity was initialized");
            }
        }

        public override void delete(User entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.delete(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.delete(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ClassHourDAOSchool nor ClassHourDAOUniversity was initialized");
            }
        }

        public override User getById(int id)
        {
            User entity;
            if (wrappedDAOSchool != null)
            {
                entity = wrappedAbstractDAOSchool.getById(id);
            }
            else if (wrappedDAOUniversity != null)
            {
                entity = wrappedAbstractDAOUniversity.getById(id);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ClassHourDAOSchool nor ClassHourDAOUniversity was initialized");
            }

            return entity;
        }

        public override IList<User> getAll()
        {
            IList<User> records;
            if (wrappedDAOSchool != null)
            {
                records = wrappedAbstractDAOSchool.getAll();
            }
            else if (wrappedDAOUniversity != null)
            {
                records = wrappedAbstractDAOUniversity.getAll();
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ClassHourDAOSchool nor ClassHourDAOUniversity was initialized");
            }

            return records;
        }

    }
}
